<template>
  <Form :model="data" :rules="rules" #if(option.dialogMode!='onecolumn')mode="#(option.dialogMode)" #end ref="form">
#for(column:table.Columns)#if(column.isVisibleInForm())
    <FormItem label="#(column.Name)" prop="#(column.field)"#if(column.ControlType=='TextArea') single#end#if(column.isMandatory()) required#end>
      #switch(column.ControlType)
      #case('Select')
      <Select v-model="data.#(column.field)" #(column.viewOpt) filterable></Select>
      #case('Checkbox')
      <Checkbox v-model="data.#(column.field)" #(column.viewOpt) ></Checkbox>
      #case('Radio')
      <Radio v-model="data.#(column.field)" #(column.viewOpt) ></Radio>
      #case('TextArea')
      <Textarea v-model="data.#(column.field)" rows="3" v-autosize v-wordcount="#(column.length)"></textarea>
      #case('Date')
      <DatePicker v-model="data.#(column.field)"></DatePicker>
      #case('Time')
      <DatePicker v-model="data.#(column.field)" type="time"></DatePicker>
      #case('DateTime')
      <DatePicker v-model="data.#(column.field)" type="datetime"></DatePicker>
      #case('Switch')
      <Switch v-model="data.#(column.field)"/>
      #case('SwitchList')
      <SwitchList v-model="data.#(column.field)" #(column.viewOpt) />
      #case('Number')
      <NumberInput v-model="data.#(column.field)" :precision="2"></NumberInput>
      #case('Map')
      <!--可以使用 v-model:address 绑定地址 -->
      <Map v-model="data.#(column.field)"></NumberInput>
      #case('Image')
      <File v-model="data.#(column.field)" accept="*.jpg,*.png" maxSize="3M"/>
      #case('File')
      <File v-model="data.#(column.field)" maxSize="10M"/>
      #default
      #if(column.DataType==12)
      <DatePicker v-model="data.#(column.field)" type="datetime"></DatePicker>
      #else
      <Input type="text" v-model="data.#(column.field)"/>
      #end
      #end
    </FormItem>
#end
#end
  </Form>
</template>
<script setup>
import {ajax} from 'framework'
import {ref,reactive} from 'vue'
import {message} from 'heyui'

//事件
//Props Open方法传入的参数
const {params,dialog} = defineProps({
  params:Object,
  dialog:Object
})

//dialog.cancel() 方法可以直接关闭对话框
//打开Dialog时进行初始化
let isAdd=false
if(params){
    //通知Dialog处于loading状态
    dialog.loading(true)
    ajax.get("#(firstToLowerCase(table.name))/detail", params).then(r => {
        Object.assign(data, r.data)
        dialog.loading(false)
    })
}else{
    isAdd = true
}

//属性和状态
let form=ref(null)

//表单数据
const data=reactive({
    #for(column:table.columns)
    #if(column.isVisibleInForm())
    #(column.field):null,
    #end
    #end
})

//验证规则
const rules=#(table.rules)

/**
* 提交数据
*/
dialog.onSubmit(()=>{
    let validResult=form.value.valid();
    if(validResult.result){
         dialog.loading(true)
        ajax.post(isAdd?'#(firstToLowerCase(table.name))/add':'#(firstToLowerCase(table.name))/save',data).then(r=>{
            message("已保存")
            dialog.success(r.data)
        }).catch(e=>{
            message.error(e.message)
        }).finally(f=> dialog.loading(false))
    }
})

</script>
